package com.learn.rabbitmq.controller;

import com.learn.rabbitmq.config.DelayedQueueConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 发送延迟消息
 */
@Slf4j
@RestController
@RequestMapping("/ttl")
public class SendMesController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //开始发消息 基于插件 加过期时间
    @GetMapping("/sendTTLMsg/{message}/{delayedTime}")
    public void sendMessage(@PathVariable String message, @PathVariable Integer delayedTime) {
        log.info("当前时间:{},发送一条消息给延迟队列:{}", new Date().toString(), message);

        rabbitTemplate.convertAndSend(DelayedQueueConfig.DELAYED_EXCHANGES_NAME,
                DelayedQueueConfig.DELAYED_ROUTING_KEY_NAME,
                "延迟" + delayedTime + "s的消息:" + message,
                msg -> {
                    //发送消息，设置延迟时长
                    msg.getMessageProperties().setDelay(delayedTime);
                    return msg;
                });

    }


}
